package com.ruoyi.hcmain.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hcmain.mapper.HcStuffMapper;
import com.ruoyi.hcmain.domain.HcStuff;
import com.ruoyi.hcmain.service.IHcStuffService;

import javax.validation.Validator;
/**
 * 物料Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-13
 */
@Service
public class HcStuffServiceImpl implements IHcStuffService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    private HcStuffMapper hcStuffMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询物料
     *
     * @param stuffId 物料主键
     * @return 物料
     */
    @Override
    public HcStuff selectHcStuffByStuffId(Long stuffId)
    {
        return hcStuffMapper.selectHcStuffByStuffId(stuffId);
    }

    /**
     * 查询物料列表
     *
     * @param hcStuff 物料
     * @return 物料
     */
    @Override
    public List<HcStuff> selectHcStuffList(HcStuff hcStuff)
    {
        return hcStuffMapper.selectHcStuffList(hcStuff);
    }


    @Override
    public String importUser(List<HcStuff> userList, Boolean isUpdateSupport)
    {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (HcStuff user : userList)
        {
            try
            {
                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("sapnum", user.getSapnum());
                paramMap.put("type", user.getType());
                paramMap.put("drawno", user.getDrawno());
                paramMap.put("name", user.getName());
                // 验证是否存在这个用户
                List<HcStuff> us = hcStuffMapper.getStuffsByMap(paramMap);
                if (us.size() == 0) {
//                    HcStuff u=us.get(0);
//                    if (StringUtils.isNull(u))
//                    {
                    BeanValidators.validateWithException(validator, user);
                    hcStuffMapper.insertHcStuff(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + user.getName() + " 导入成功");
//                    }
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
                    hcStuffMapper.updateHcStuff(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + user.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + user.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据 " + user.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    /**
     * 新增物料
     *
     * @param hcStuff 物料
     * @return 结果
     */
    @Override
    public int insertHcStuff(HcStuff hcStuff)
    {
        return hcStuffMapper.insertHcStuff(hcStuff);
    }

    /**
     * 修改物料
     *
     * @param hcStuff 物料
     * @return 结果
     */
    @Override
    public int updateHcStuff(HcStuff hcStuff)
    {
        return hcStuffMapper.updateHcStuff(hcStuff);
    }

    /**
     * 批量删除物料
     *
     * @param stuffIds 需要删除的物料主键
     * @return 结果
     */
    @Override
    public int deleteHcStuffByStuffIds(Long[] stuffIds)
    {
        return hcStuffMapper.deleteHcStuffByStuffIds(stuffIds);
    }

    /**
     * 删除物料信息
     *
     * @param stuffId 物料主键
     * @return 结果
     */
    @Override
    public int deleteHcStuffByStuffId(Long stuffId)
    {
        return hcStuffMapper.deleteHcStuffByStuffId(stuffId);
    }
}
